import ajax from '../../libs/ajax.js';
import tools from '../../libs/tools.js';

let txtqtitle = document.getElementById('txtqtitle');
let btnquery = document.getElementById('btnquery');
let btncz = document.getElementById('btncz');
let btnfh = document.getElementById('btnfh');
let tbdata = document.getElementById('tbdata');

let sz = [];
let cs1 = {
  info: txtqtitle.value
};
let cs2 = {
  pageNumber: 1,
  pageSize: 7
};
function glliuy() {
  let cs = tools.concatJson(cs1, cs2);
  ajax.send('/message/manage/queryAdminAll', cs, (data) => {
    if (data.success) {
      sz = data.list;
      cs2 = data.page;
      showdata();
      showpage();
    }
  });
}
btnquery.addEventListener('click', () => {
  cs2.pageNumber = 1;
  cs1.info = txtqtitle.value;
  glliuy();
});

btncz.addEventListener('click',()=>{
  cs2.pageNumber = 1;
  txtqtitle.value='';
  cs1.info = txtqtitle.value;
  glliuy();
});
function showdata() {
  tbdata.innerHTML = '';
  for (let i = 0; i < sz.length; i++) {
    const et = sz[i];
    let tr = document.createElement('tr');
    //用户名
    let td = document.createElement('td');
    tr.append(td);
    td.append(et.user.nickname);

    //昵称
    td = document.createElement('td');
    tr.append(td);
    td.append(et.user.username);
    //标题
    td = document.createElement('td');
    tr.append(td);
    td.append(et.title);

    //是否屏蔽
    td = document.createElement('td');
    tr.append(td);
    td.append(et.disable);
    if (et.disable == 'n') {
      td.innerHTML = '正常';
    } else {
      td.innerHTML = '被屏蔽';
    }

    //是否被举报
    td = document.createElement('td');
    tr.append(td);
    td.append(et.examine);
    if (et.examine == 'n') {
      td.innerHTML = '正常';
    } else {
      td.innerHTML = '被举报';
    }

    //点击量
    td = document.createElement('td');
    tr.append(td);
    td.append(et.hits);

    //信息最后修改时间
    td = document.createElement('td');
    tr.append(td);
    td.append(tools.FormData(et.lastupdate));

    // 操作
    td = document.createElement('td');
    tr.append(td);
    // 屏蔽按钮
    let btn1 = document.createElement('span');
    btn1.append('屏蔽');
    btn1.classList.add('btn', 'btn-primary', 'btn-sm', 'm-1');
    btn1.setAttribute('data-bs-toggle', 'modal');
    btn1.setAttribute('data-bs-target', '#pbDialog');
    td.append(btn1);
    btn1.addEventListener('click', () => {
      let txtatitle = document.getElementById('txtatitle');
      let btnadd = document.getElementById('btnadd');
      let cs3 = {
        disableReason: txtatitle.value,
        disable: ''
      };
      btnadd.addEventListener('click', () => {
        ajax.send('/message/manage/queryAdminAll', cs3, (data) => {
          if (data.success) {
            data.list.disableReason = cs3.disableReason;
            data.list.disable = 'y';
            btn1.innerHTML = '被屏蔽';
          }
        });
      });
    });

    // 查看举报原因按钮
    let btn2 = document.createElement('span');
    btn2.append('查看举报原因');
    btn2.classList.add('btn', 'btn-danger', 'btn-sm', 'm-1');

    btn2.setAttribute('data-bs-toggle', 'modal');
    btn2.setAttribute('data-bs-target', '#jbyyDialog');

    td.append(btn2);
    btn2.addEventListener('click', () => {
      let xj1 = {
        umid: et.umid
      };
      let xj2 = {
        pageNumber: 1,
        pageSize: 1
      };
      let xj3 = [];
      ajax.send(
        '/message/manage/queryExamineInfo',
        tools.concatJson(xj1, xj2),
        (data) => {
          xj3 = data.list;
          xj2 = data.page;
          console.log('xj3', xj3);
          zxx();
        }
      );
      function zxx() {
        for (let i = 0; i < xj3.length; i++) {
          const e = xj3[i];
          let imgLogo01 = document.getElementById('imgLogo01');
          imgLogo01.src = `${e.img}`;
          let mc = document.getElementById('mc');
          mc.innerHTML = e.nickname;
          let jbyy = document.getElementById('jbyy');
          jbyy.innerHTML = e.info;
        }
      }
    });

    tbdata.append(tr);
  }
}

//#region 切换评论
let btnqh = document.getElementById('btnqh');
btnqh.addEventListener('click', () => {
  location.href = './plym.html';
});
//#endregion

//#region 返回
btnfh.addEventListener('click', () => {
  location.href = './guanliyuan.html';
});
//#endregion

//#region 分页
let pages = document.querySelectorAll('#navpage>span');
function showpage() {
  pages[1].innerHTML = `
当前页/总页数/记录数：
${cs2.pageNumber}/${cs2.pageCount}/${cs2.total}
`;
}

pages[0].addEventListener('click', () => {
  cs2.pageNumber--;
  if (cs2.pageNumber < 1) {
    cs2.pageNumber = 1;
    return;
  }
  glliuy();
});

pages[2].addEventListener('click', () => {
  cs2.pageNumber++;
  if (cs2.pageNumber > cs2.pageCount) {
    cs2.pageNumber = cs2.pageCount;
    return;
  }
  glliuy();
});
//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

glliuy();
